home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / WINSYS.PAK / GEOMETRY.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  27KB  |  1,414 lines

  1. //----------------------------------------------------------------------------
  2. // Borland WinSys Library
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.14  $
  6. //
  7. // Classes for window system geometry
  8. //----------------------------------------------------------------------------
  9. #if !defined(WINSYS_GEOMETRY_H)
  10. #define WINSYS_GEOMETRY_H
  11.  
  12. #if !defined(WINSYS_DEFS_H)
  13. # include <winsys/defs.h>
  14. #endif
  15. #if !defined(CLASSLIB_OBJSTRM_H)
  16. # include <classlib/objstrm.h>    // Need persist streaming classes & operators
  17. #endif
  18.  
  19. #if defined(BI_NAMESPACE)
  20. namespace ClassLib {
  21. #endif
  22.  
  23. //
  24. // Forward declare some of the classes defined in this file
  25. //
  26. class _WSYSCLASS TSize;
  27. class _WSYSCLASS TRect;
  28.  
  29. //
  30. // Integer square root for area calculations. Is fairly fast.
  31. //
  32. int16  Sqrt(int32 val);
  33.  
  34. //
  35. // Get base window system geometry structs compatible with MSW
  36. //
  37. #if defined(BI_PLAT_MSW)
  38. # if !defined(WINSYS_WSYSINC_H)
  39. #   include <winsys/wsysinc.h>
  40. # endif
  41. # if defined(BI_PLAT_WIN16) && !defined(SERVICES_OLE2INC_H)
  42. #   include <services/ole2inc.h>
  43. # endif
  44. #else
  45.   struct tagPOINT {
  46.     int x;
  47.     int y;
  48.   };
  49.   struct POINTL {
  50.     long x;
  51.     long y;
  52.   };
  53.   struct tagSIZE {
  54.     int cx;
  55.     int cy;
  56.   };
  57.   struct tagRECT {
  58.     int left;
  59.     int top;
  60.     int right;
  61.     int bottom;
  62.   };
  63. #endif
  64.  
  65.   struct tPOINTF {
  66.     float x;
  67.     float y;
  68.   };
  69.  
  70.  
  71. //
  72. // class TPoint
  73. // ~~~~~ ~~~~~~
  74. class _WSYSCLASS TPoint : public tagPOINT {
  75.   public:
  76.     // Constructors
  77.     //
  78.     TPoint();
  79.     TPoint(int _x, int _y);
  80.     TPoint(const tagPOINT far& point);
  81.     TPoint(const TPoint far& point);
  82.     TPoint(const tagSIZE far& size);
  83.     TPoint(uint32 u32);
  84.  
  85.     // Information functions/operators
  86.     //
  87.     bool        operator ==(const TPoint& other) const;
  88.     bool        operator !=(const TPoint& other) const;
  89.     int         X() const;
  90.     int         Y() const;
  91.     int         Magnitude() const;
  92.  
  93.     // Functions/binary-operators that return points or sizes
  94.     //
  95.     TPoint      OffsetBy(int dx, int dy) const;
  96.     TPoint      operator +(const TSize& size) const;
  97.     TSize       operator -(const TPoint& point) const;
  98.     TPoint      operator -(const TSize& size) const;
  99.     TPoint      operator -() const;
  100.  
  101.     // Functions/assignement-operators that modify this point
  102.     //
  103.     TPoint&     Offset(int dx, int dy);
  104.     TPoint&     operator +=(const TSize& size);
  105.     TPoint&     operator -=(const TSize& size);
  106. };
  107.  
  108. ipstream& _WSYSFUNC operator >>(ipstream& is, TPoint& p);
  109. istream& _WSYSFUNC operator >>(istream& is, TPoint& p);
  110. opstream& _WSYSFUNC operator <<(opstream& os, const TPoint& p);
  111. ostream& _WSYSFUNC operator <<(ostream& os, const TPoint& p);
  112.  
  113. //
  114. // class TPointL
  115. // ~~~~~ ~~~~~~~
  116. class _WSYSCLASS TPointL : public POINTL {
  117.   public:
  118.     // Constructors
  119.     //
  120.     TPointL();
  121.     TPointL(long _x, long _y);
  122.     TPointL(const POINTL far& point);
  123.     TPointL(const TPointL far& point);
  124.  
  125.     // Information functions/operators
  126.     //
  127.     bool        operator ==(const TPointL& other) const;
  128.     bool        operator !=(const TPointL& other) const;
  129.     long        X() const;
  130.     long        Y() const;
  131.  
  132.     // Functions/binary-operators that return points or sizes
  133.     //
  134.     TPointL     OffsetBy(long dx, long dy) const;
  135.     TPointL     operator +(const TSize& size) const;
  136.     TPointL     operator -(const TPointL& point) const;
  137.     TPointL     operator -(const TSize& size) const;
  138.     TPointL     operator -() const;
  139.  
  140.     // Functions/assignement-operators that modify this point
  141.     //
  142.     TPointL&    Offset(long dx, long dy);
  143.     TPointL&    operator +=(const TSize& size);
  144.     TPointL&    operator -=(const TSize& size);
  145. };
  146.  
  147. ipstream& _WSYSFUNC operator >>(ipstream& is, TPointL& p);
  148. istream& _WSYSFUNC operator >>(istream& is, TPointL& p);
  149. opstream& _WSYSFUNC operator <<(opstream& os, const TPointL& p);
  150. ostream& _WSYSFUNC operator <<(ostream& os, const TPointL& p);
  151.  
  152. //
  153. // class TPointF
  154. // ~~~~~ ~~~~~~~
  155. class _WSYSCLASS TPointF : public tPOINTF {
  156.   public:
  157.     // Constructors
  158.     //
  159.     TPointF();
  160.     TPointF(float _x, float _y);
  161. //    TPointF(const tagPOINTF far& point) {x = point.x; y = point.y;}
  162.     TPointF(const TPointF far& point);
  163.  
  164.     // Information functions/operators
  165.     //
  166.     bool        operator ==(const TPointF& other) const;
  167.     bool        operator !=(const TPointF& other) const;
  168.     float       X() const;
  169.     float       Y() const;
  170.  
  171.     // Functions/binary-operators that return points or sizes
  172.     //
  173.     TPointF     OffsetBy(float dx, float dy) const;
  174.     TPointF     operator +(const TPointF& size) const;
  175.     TPointF     operator -(const TPointF& point) const;
  176.     TPointF     operator -() const;
  177.  
  178.     // Functions/assignement-operators that modify this point
  179.     //
  180.     TPointF&    Offset(float dx, float dy);
  181.     TPointF&    operator +=(const TPointF& size);
  182.     TPointF&    operator -=(const TPointF& size);
  183. };
  184.  
  185. ipstream& _WSYSFUNC operator >>(ipstream& is, TPointF& p);
  186. istream& _WSYSFUNC operator >>(istream& is, TPointF& p);
  187. opstream& _WSYSFUNC operator <<(opstream& os, const TPointF& p);
  188. ostream& _WSYSFUNC operator <<(ostream& os, const TPointF& p);
  189.  
  190. //
  191. // class TSize
  192. // ~~~~~ ~~~~~
  193. //
  194. class _WSYSCLASS TSize : public tagSIZE {
  195.   public:
  196.     // Constructors
  197.     //
  198.     TSize();
  199.     TSize(int dx, int dy);
  200.     TSize(const tagPOINT far& point);
  201.     TSize(const tagSIZE far& size);
  202.     TSize(const TSize far& size);
  203.     TSize(uint32 dw);
  204.  
  205.     // Information functions/operators
  206.     //
  207.     bool        operator ==(const TSize& other) const;
  208.     bool        operator !=(const TSize& other) const;
  209.     int         X() const;
  210.     int         Y() const;
  211.     int         Magnitude() const;
  212.  
  213.     // Functions/binary-operators that return sizes
  214.     //
  215.     TSize      operator +(const TSize& size) const;
  216.     TSize      operator -(const TSize& size) const;
  217.     TSize      operator -() const;
  218.  
  219.     // Functions/assignement-operators that modify this size
  220.     //
  221.     TSize&     operator +=(const TSize& size);
  222.     TSize&     operator -=(const TSize& size);
  223. };
  224.  
  225. ipstream& _WSYSFUNC operator >>(ipstream& is, TSize& s);
  226. istream& _WSYSFUNC operator >>(istream& os, TSize& s);
  227. opstream& _WSYSFUNC operator <<(opstream& os, const TSize& s);
  228. ostream& _WSYSFUNC operator <<(ostream& os, const TSize& s);
  229.  
  230. //
  231. // class TRect
  232. // ~~~~~ ~~~~~
  233. class _WSYSCLASS TRect : public tagRECT {
  234.   public:
  235.     // Constructors
  236.     //
  237.     TRect();
  238.     TRect(const tagRECT far& rect);
  239.     TRect(const TRect far& rect);
  240.     TRect(int _left, int _top, int _right, int _bottom);
  241.     TRect(const TPoint& upLeft, const TPoint& loRight);
  242.     TRect(const TPoint& origin, const TSize& extent);
  243.  
  244.     // (Re)Initializers
  245.     //
  246.     void        SetNull();
  247.     void        SetEmpty();
  248.     void        Set(int _left, int _top, int _right, int _bottom);
  249.     void        SetWH(int _left, int _top, int w, int h);
  250.  
  251.     // Type Conversion operators
  252.     //
  253.     operator    const TPoint*() const;
  254.     operator    TPoint*();
  255.  
  256.     // Testing functions
  257.     //
  258.     bool        IsEmpty() const;
  259.     bool        IsNull() const;
  260.     bool        operator ==(const TRect& other) const;
  261.     bool        operator !=(const TRect& other) const;
  262.  
  263.     // Information/access functions (const and non-const)
  264.     //
  265.     int         Left() const;
  266.     int         X() const;
  267.     int         Top() const;
  268.     int         Y() const;
  269.     int         Right() const;
  270.     int         Bottom() const;
  271.  
  272.     const TPoint& TopLeft() const;
  273.     TPoint&     TopLeft();
  274.     TPoint      TopRight() const;
  275.     TPoint      BottomLeft() const;
  276.     const TPoint& BottomRight() const;
  277.     TPoint&     BottomRight();
  278.  
  279.     int         Width() const;
  280.     int         Height() const;
  281.     TSize       Size() const;
  282.     long        Area() const;
  283.  
  284.     bool        Contains(const TPoint& point) const;
  285.     bool        Contains(const TRect& other) const;
  286.     bool        Touches(const TRect& other) const;
  287.     TRect       OffsetBy(int dx, int dy) const;
  288.     TRect       operator +(const TSize& size) const;
  289.     TRect       operator -(const TSize& size) const;
  290.     TRect       MovedTo(int x, int y);
  291.     TRect       InflatedBy(int dx, int dy) const;
  292.     TRect       InflatedBy(const TSize& size) const;
  293.     TRect       Normalized() const;
  294.     TRect       operator &(const TRect& other) const;
  295.     TRect       operator |(const TRect& other) const;
  296.  
  297.     int         Subtract(const TRect& other, TRect result[]) const;
  298.  
  299.     // Manipulation functions/operators
  300.     //
  301.     TRect&      Normalize();
  302.     TRect&      Offset(int dx, int dy);
  303.     TRect&      operator +=(const TSize& delta);
  304.     TRect&      operator -=(const TSize& delta);
  305.     TRect&      MoveTo(int x, int y);
  306.     TRect&      Inflate(int dx, int dy);
  307.     TRect&      Inflate(const TSize& delta);
  308.     TRect&      operator &=(const TRect& other);
  309.     TRect&      operator |=(const TRect& other);
  310. };
  311.  
  312. ipstream& _WSYSFUNC operator >>(ipstream& is, TRect& r);
  313. istream& _WSYSFUNC operator >>(istream& is, TRect& r);
  314. opstream& _WSYSFUNC operator <<(opstream& os, const TRect& r);
  315. ostream& _WSYSFUNC operator <<(ostream& os, const TRect& r);
  316.  
  317. #if defined(BI_NAMESPACE)
  318. }     // namespace ClassLib
  319. #endif
  320.  
  321. //----------------------------------------------------------------------------
  322. // Inlines
  323. //
  324.  
  325. //----------------------------------------------------------------------------
  326. // TPoint
  327. //
  328.  
  329. //
  330. // Construct an uninitialized point.
  331. //
  332. inline
  333. TPoint::TPoint()
  334. {
  335. }
  336.  
  337. //
  338. // Initialize a point a specific location.
  339. //
  340. inline
  341. TPoint::TPoint(int _x, int _y)
  342. {
  343.   x = _x;
  344.   y = _y;
  345. }
  346.  
  347. //
  348. // Make a copy of the point.
  349. //
  350. inline
  351. TPoint::TPoint(const tagPOINT far& point)
  352. {
  353.   x = point.x;
  354.   y = point.y;
  355. }
  356.  
  357. //
  358. // Make a copy of the point.
  359. //
  360. inline
  361. TPoint::TPoint(const TPoint far& point)
  362. {
  363.   x = point.x;
  364.   y = point.y;
  365. }
  366.  
  367. //
  368. // Create a point whose coordinates is the size.
  369. //
  370. inline
  371. TPoint::TPoint(const tagSIZE far& size)
  372. {
  373.   x = size.cx;
  374.   y = size.cy;
  375. }
  376.  
  377. //
  378. // Create a point object from a packed point.
  379. // This assumes the x coordinate is in the low word of a double word and
  380. // the y coordinate is in the high word.
  381. //
  382. inline
  383. TPoint::TPoint(uint32 u32)
  384. {
  385.   x = LoInt16(u32);
  386.   y = HiInt16(u32);
  387. }
  388.  
  389. //
  390. // Return the x coordinate of the point.
  391. //
  392. inline int
  393. TPoint::X() const
  394. {
  395.   return x;
  396. }
  397.  
  398. //
  399. // Return the Y coordinate of the point.
  400. //
  401. inline int
  402. TPoint::Y() const
  403. {
  404.   return y;
  405. }
  406.  
  407. //
  408. // Return a point whose coordinate is the coordinate of the current point
  409. // plus passed in coordinates.
  410. //
  411. inline TPoint
  412. TPoint::OffsetBy(int dx, int dy) const
  413. {
  414.   return TPoint(x + dx, y + dy);
  415. }
  416.  
  417. //
  418. // Return a point whose coordinate is the opposite (negative) of the current
  419. // location.
  420. //
  421. inline TPoint
  422. TPoint::operator -() const
  423. {
  424.   return TPoint(-x, -y);
  425. }
  426.  
  427. //
  428. // Return true if the location of the two points are the same.
  429. //
  430. inline bool
  431. TPoint::operator ==(const TPoint& other) const
  432. {
  433.   return ToBool(other.x == x && other.y == y);
  434. }
  435.  
  436. //
  437. // Return true if the locations are not the same.
  438. //
  439. inline bool
  440. TPoint::operator !=(const TPoint& other) const
  441. {
  442.   return ToBool(other.x != x || other.y != y);
  443. }
  444.  
  445. //
  446. // Return a point whose coordinate is the current location plus the
  447. // size.
  448. //
  449. inline TPoint
  450. TPoint::operator +(const TSize& size) const
  451. {
  452.   return TPoint(x + size.cx, y + size.cy);
  453. }
  454.  
  455. //
  456. // Return the resulting size of subtracting a location from the current location.
  457. //
  458. inline TSize
  459. TPoint::operator -(const TPoint& point) const
  460. {
  461.   return TSize(x - point.x, y - point.y);
  462. }
  463.  
  464. //
  465. // Return the resulting point of subtracting a size from the current location.
  466. //
  467. inline TPoint
  468. TPoint::operator -(const TSize& size) const
  469. {
  470.   return TPoint(x - size.cx, y - size.cy);
  471. }
  472.  
  473. //
  474. // Adjust the current location by the offsets passed.
  475. // Return the resulting location.
  476. //
  477. inline TPoint&
  478. TPoint::Offset(int dx, int dy)
  479. {
  480.   x += dx;
  481.   y += dy;
  482.   return *this;
  483. }
  484.  
  485. //
  486. // Adjust the current location by the size passed.
  487. // Return the resulting location.
  488. //
  489. inline TPoint&
  490. TPoint::operator +=(const TSize& size)
  491. {
  492.   x += size.cx;
  493.   y += size.cy;
  494.   return *this;
  495. }
  496.  
  497. //
  498. // Subtract the current location by the size passed.
  499. // Return the resulting location.
  500. //
  501. inline TPoint&
  502. TPoint::operator -=(const TSize& size)
  503. {
  504.   x -= size.cx;
  505.   y -= size.cy;
  506.   return *this;
  507. }
  508.  
  509. //----------------------------------------------------------------------------
  510. // TPointL
  511. //
  512.  
  513. //
  514. // Default constructor that does nothing.
  515. //
  516. inline
  517. TPointL::TPointL()
  518. {
  519. }
  520.  
  521. //
  522. // Construct the point to a specific location.
  523. //
  524. inline
  525. TPointL::TPointL(long _x, long _y)
  526. {
  527.   x = _x;
  528.   y = _y;
  529. }
  530.  
  531. //
  532. // Alias constructor that initializes from an existing point.
  533. //
  534. inline
  535. TPointL::TPointL(const POINTL far& point)
  536. {
  537.   x = point.x;
  538.   y = point.y;
  539. }
  540.  
  541. //
  542. // Copy constructor. Makes a copy of the location.
  543. //
  544. inline
  545. TPointL::TPointL(const TPointL far& point)
  546. {
  547.   x = point.x;
  548.   y = point.y;
  549. }
  550.  
  551. //
  552. // Return the X component of the point.
  553. //
  554. inline long
  555. TPointL::X() const
  556. {
  557.   return x;
  558. }
  559.  
  560. //
  561. // Return the Y component of the point.
  562. //
  563. inline long
  564. TPointL::Y() const
  565. {
  566.   return y;
  567. }
  568.  
  569. //
  570. // Return the new point (x+dx, y+dy).
  571. //
  572. inline TPointL
  573. TPointL::OffsetBy(long dx, long dy) const
  574. {
  575.   return TPointL(x + dx, y + dy);
  576. }
  577.  
  578. //
  579. // Return the negative of the point.
  580. //
  581. inline TPointL
  582. TPointL::operator -() const
  583. {
  584.   return TPointL(-x, -y);
  585. }
  586.  
  587. //
  588. // Return true if positions are the same.
  589. //
  590. inline bool
  591. TPointL::operator ==(const TPointL& other) const
  592. {
  593.   return ToBool(other.x == x && other.y == y);
  594. }
  595.  
  596. //
  597. // Return true if the positions are not the same.
  598. //
  599. inline bool
  600. TPointL::operator !=(const TPointL& other) const
  601. {
  602.   return ToBool(other.x != x || other.y != y);
  603. }
  604.  
  605. //
  606. // Return the new point (x+cx, y+cy).
  607. //
  608. inline TPointL
  609. TPointL::operator +(const TSize& size) const
  610. {
  611.   return TPointL(x + size.cx, y + size.cy);
  612. }
  613.  
  614. //
  615. // Return the difference between the two points.
  616. //
  617. inline TPointL
  618. TPointL::operator -(const TPointL& point) const
  619. {
  620.   return TPointL(x - point.x, y - point.y);
  621. }
  622.  
  623. //
  624. // Return the new point (x-cx, y-cy).
  625. //
  626. inline TPointL
  627. TPointL::operator -(const TSize& size) const
  628. {
  629.   return TPointL(x - size.cx, y - size.cy);
  630. }
  631.  
  632. //
  633. // Return the point (x+dx, y+dy).
  634. //
  635. inline TPointL&
  636. TPointL::Offset(long dx, long dy)
  637. {
  638.   x += dx;
  639.   y += dy;
  640.   return *this;
  641. }
  642.  
  643. //
  644. // Return the point (x+cx, y+cy).
  645. //
  646. inline TPointL&
  647. TPointL::operator +=(const TSize& size)
  648. {
  649.   x += size.cx;
  650.   y += size.cy;
  651.   return *this;
  652. }
  653.  
  654. //
  655. // Return the point (x-cx, y-cy).
  656. //
  657. inline TPointL&
  658. TPointL::operator -=(const TSize& size)
  659. {
  660.   x -= size.cx;
  661.   y -= size.cy;
  662.   return *this;
  663. }
  664.  
  665.  
  666. //----------------------------------------------------------------------------
  667. // TPointF
  668. //
  669.  
  670. //
  671. // Default constructor that does nothing.
  672. //
  673. inline
  674. TPointF::TPointF()
  675. {
  676. }
  677.  
  678. //
  679. // Constructor that initializes the location.
  680. //
  681. inline
  682. TPointF::TPointF(float _x, float _y)
  683. {
  684.   x = _x;
  685.   y = _y;
  686. }
  687.  
  688. //
  689. // Constructor that copies the location.
  690. //
  691. inline
  692. TPointF::TPointF(const TPointF far& point)
  693. {
  694.   x = point.x;
  695.   y = point.y;
  696. }
  697.  
  698. //
  699. // Return X component of the point.
  700. //
  701. inline float
  702. TPointF::X() const
  703. {
  704.   return x;
  705. }
  706.  
  707. //
  708. // Return Y component of the point.
  709. //
  710. inline float
  711. TPointF::Y() const
  712. {
  713.   return y;
  714. }
  715.  
  716. //
  717. // Move the point by an offset.
  718. //
  719. inline TPointF
  720. TPointF::OffsetBy(float dx, float dy) const
  721. {
  722.   return TPointF(x + dx, y + dy);
  723. }
  724.  
  725. //
  726. // Return the negative of the point.
  727. //
  728. inline TPointF
  729. TPointF::operator -() const
  730. {
  731.   return TPointF(-x, -y);
  732. }
  733.  
  734. //
  735. // Return true if the points are at the same location.
  736. //
  737. inline bool
  738. TPointF::operator ==(const TPointF& other) const
  739. {
  740.   return ToBool(other.x == x && other.y == y);
  741. }
  742.  
  743. //
  744. // Return true if the points are not at the same location.
  745. //
  746. inline bool
  747. TPointF::operator !=(const TPointF& other) const
  748. {
  749.   return ToBool(other.x != x || other.y != y);
  750. }
  751.  
  752. //
  753. // Return a new point (x+cx, y+cy).
  754. //
  755. inline TPointF
  756. TPointF::operator +(const TPointF& size) const
  757. {
  758.   return TPointF(x + size.x, y + size.y);
  759. }
  760.  
  761. //
  762. // Return a new point subtracted from the current point.
  763. //
  764. inline TPointF
  765. TPointF::operator -(const TPointF& point) const
  766. {
  767.   return TPointF(x - point.x, y - point.y);
  768. }
  769.  
  770. //
  771. // Move the point by an offset.
  772. //
  773. inline TPointF&
  774. TPointF::Offset(float dx, float dy)
  775. {
  776.   x += dx;
  777.   y += dy;
  778.   return *this;
  779. }
  780.  
  781. //
  782. // Return the new point moved by the offset.
  783. //
  784. inline TPointF&
  785. TPointF::operator +=(const TPointF& size)
  786. {
  787.   x += size.x;
  788.   y += size.y;
  789.   return *this;
  790. }
  791.  
  792. //
  793. // Return the new point subtracted from the current.
  794. //
  795. inline TPointF&
  796. TPointF::operator -=(const TPointF& size)
  797. {
  798.   x -= size.x;
  799.   y -= size.y;
  800.   return *this;
  801. }
  802.  
  803. //----------------------------------------------------------------------------
  804. // TRect
  805. //
  806.  
  807. //
  808. // Default constructor that does nothing.
  809. //
  810. inline
  811. TRect::TRect()
  812. {
  813. }
  814.  
  815. //
  816. // Create an empty rectangle.
  817. //
  818. inline void
  819. TRect::SetEmpty()
  820. {
  821.   SetNull();
  822. }
  823.  
  824. //
  825. // Return an array of two points (upperleft and bottomright)
  826. //
  827. inline
  828. TRect::operator const TPoint*() const
  829. {
  830.   return (const TPoint*)this;
  831. }
  832.  
  833. //
  834. // Return an array of two points (upperleft and bottomright)
  835. //
  836. inline
  837. TRect::operator TPoint*()
  838. {
  839.   return (TPoint*)this;
  840. }
  841.  
  842. //
  843. // Return the left value.
  844. //
  845. inline int
  846. TRect::Left() const
  847. {
  848.   return left;
  849. }
  850.  
  851. //
  852. // Return the left value.
  853. //
  854. inline int
  855. TRect::X() const
  856. {
  857.   return left;
  858. }
  859.  
  860. //
  861. // Return the top value.
  862. //
  863. inline int
  864. TRect::Top() const
  865. {
  866.   return top;
  867. }
  868.  
  869. //
  870. // Return the top value.
  871. //
  872. inline int
  873. TRect::Y() const
  874. {
  875.   return top;
  876. }
  877.  
  878. //
  879. // Return the right value.
  880. //
  881. inline int
  882. TRect::Right() const
  883. {
  884.   return right;
  885. }
  886.  
  887. //
  888. // Return the bottom value.
  889. //
  890. inline int
  891. TRect::Bottom() const
  892. {
  893.   return bottom;
  894. }
  895.  
  896. //
  897. // Return the upperleft point.
  898. //
  899. inline const TPoint&
  900. TRect::TopLeft() const
  901. {
  902.   return *(TPoint*)&left;
  903. }
  904.  
  905. //
  906. // Return the upperleft point.
  907. //
  908. inline TPoint&
  909. TRect::TopLeft()
  910. {
  911.   return *(TPoint*)&left;
  912. }
  913.  
  914. //
  915. // Return the upperright point.
  916. //
  917. inline TPoint
  918. TRect::TopRight() const
  919. {
  920.   return TPoint(right, top);
  921. }
  922.  
  923. //
  924. // Return the bottomleft point.
  925. //
  926. inline TPoint
  927. TRect::BottomLeft() const
  928. {
  929.   return TPoint(left, bottom);
  930. }
  931.  
  932. //
  933. // Return the bottomright point.
  934. //
  935. inline const TPoint&
  936. TRect::BottomRight() const
  937. {
  938.   return *(TPoint*)&right;
  939. }
  940.  
  941. //
  942. // Return the bottomright point.
  943. //
  944. inline TPoint&
  945. TRect::BottomRight()
  946. {
  947.   return *(TPoint*)&right;
  948. }
  949.  
  950. //
  951. // Return the width of the rectangle.
  952. //
  953. inline int
  954. TRect::Width() const
  955. {
  956.   return right-left;
  957. }
  958.  
  959. //
  960. // Return the height of the rectangle.
  961. //
  962. inline int
  963. TRect::Height() const
  964. {
  965.   return bottom-top;
  966. }
  967.  
  968. //
  969. // Return the size of rectangle.
  970. //
  971. inline TSize
  972. TRect::Size() const
  973. {
  974.   return TSize(Width(), Height());
  975. }
  976.  
  977. //
  978. // Return the area of th rectangle.
  979. //
  980. inline long
  981. TRect::Area() const
  982. {
  983.   return long(Width())*long(Height());
  984. }
  985.  
  986. //
  987. // Set all the data members to 0.
  988. //
  989. inline void
  990. TRect::SetNull()
  991. {
  992.   left = 0;
  993.   top = 0;
  994.   right = 0;
  995.   bottom = 0;
  996. }
  997.  
  998. //
  999. // Explicitly set all of the coordinates of the rectangle.
  1000. //
  1001. inline void
  1002. TRect::Set(int _left, int _top, int _right, int _bottom)
  1003. {
  1004.   left = _left;
  1005.   top = _top;
  1006.   right = _right;
  1007.   bottom = _bottom;
  1008. }
  1009.  
  1010. //
  1011. // Determine the rectangle, given its upperleft point, width, and height
  1012. //
  1013. inline void
  1014. TRect::SetWH(int _left, int _top, int w, int h)
  1015. {
  1016.   left = _left;
  1017.   top = _top;
  1018.   right = _left + w;
  1019.   bottom = _top + h;
  1020. }
  1021.  
  1022. //
  1023. // Copy from an existing rectangle.
  1024. //
  1025. inline
  1026. TRect::TRect(const tagRECT far& rect)
  1027. {
  1028.   *(tagRECT far*)this = rect;
  1029. }
  1030.  
  1031. //
  1032. // Copy from an existing rectangle.
  1033. //
  1034. inline
  1035. TRect::TRect(const TRect far& rect)
  1036. {
  1037.   *(tagRECT far*)this = *(tagRECT far*)▭
  1038. }
  1039.  
  1040. //
  1041. // Constructor that sets all the values explicitly.
  1042. //
  1043. inline
  1044. TRect::TRect(int _left, int _top, int _right, int _bottom)
  1045. {
  1046.   Set(_left, _top, _right, _bottom);
  1047. }
  1048.  
  1049. //
  1050. // Determine a rectangle given two points.
  1051. //
  1052. inline
  1053. TRect::TRect(const TPoint& topLeft, const TPoint& bottomRight)
  1054. {
  1055.   Set(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
  1056. }
  1057.  
  1058. //
  1059. // Determine a rectangle given its upper left point and a dimension.
  1060. //
  1061. inline
  1062. TRect::TRect(const TPoint& origin, const TSize& extent)
  1063. {
  1064.   Set(origin.x, origin.y, origin.x + extent.cx, origin.y + extent.cy);
  1065. }
  1066.  
  1067. //
  1068. // Return true if the rectangle is empty.
  1069. //
  1070. inline bool
  1071. TRect::IsEmpty() const
  1072. {
  1073.   return ToBool(left >= right || top >= bottom);
  1074. }
  1075.  
  1076. //
  1077. // Return true if all of the points on the rectangle is 0.
  1078. //
  1079. inline bool
  1080. TRect::IsNull() const
  1081. {
  1082.   return ToBool(!left && !right && !top && !bottom);
  1083. }
  1084.  
  1085. //
  1086. // A rectangle is equal to another if all of their points are identical.
  1087. //
  1088. inline bool
  1089. TRect::operator ==(const TRect& other) const
  1090. {
  1091.   return ToBool(other.left==left   && other.top==top
  1092.              && other.right==right && other.bottom==bottom);
  1093. }
  1094.  
  1095. //
  1096. // Return the opposite of operator ==.
  1097. //
  1098. inline bool
  1099. TRect::operator !=(const TRect& other) const
  1100. {
  1101.   return ToBool(!(other==*this));
  1102. }
  1103.  
  1104. //
  1105. // Return true if the rectangle contains the point.
  1106. //
  1107. inline bool
  1108. TRect::Contains(const TPoint& point) const
  1109. {
  1110.   return ToBool(point.x >= left && point.x < right
  1111.              && point.y >= top  && point.y < bottom);
  1112. }
  1113.  
  1114. //
  1115. // Return true if the rectangle complete surrounds the other.
  1116. //
  1117. inline bool
  1118. TRect::Contains(const TRect& other) const
  1119. {
  1120.   return ToBool(other.left >= left && other.right <= right
  1121.              && other.top >= top   && other.bottom <= bottom);
  1122. }
  1123.  
  1124. //
  1125. // Return true if the intersection of the two rectangles is not null.
  1126. //
  1127. inline bool
  1128. TRect::Touches(const TRect& other) const
  1129. {
  1130.   return ToBool(other.right > left && other.left < right
  1131.              && other.bottom > top && other.top < bottom);
  1132. }
  1133.  
  1134. //
  1135. // Move the points of the rectangle by the offsets.
  1136. //
  1137. inline TRect
  1138. TRect::OffsetBy(int dx, int dy) const
  1139. {
  1140.   return TRect(left+dx, top+dy, right+dx, bottom+dy);
  1141. }
  1142.  
  1143. //
  1144. // Move the points of the rectangle by the offsets.
  1145. //
  1146. inline TRect
  1147. TRect::operator +(const TSize& size) const
  1148. {
  1149.   return OffsetBy(size.cx, size.cy);
  1150. }
  1151.  
  1152. //
  1153. // Move the points of the rectangle negatively by the offsets.
  1154. //
  1155. inline TRect
  1156. TRect::operator -(const TSize& size) const
  1157. {
  1158.   return OffsetBy(-size.cx, -size.cy);
  1159. }
  1160.  
  1161. //
  1162. // Move the upper left point of the rectangle while maintaining the
  1163. // current dimension.
  1164. //
  1165. inline TRect
  1166. TRect::MovedTo(int x, int y)
  1167. {
  1168.   return TRect(x, y, x + Width(), y + Height());
  1169. }
  1170.  
  1171. //
  1172. // Inflate the size of the rectangle.
  1173. //
  1174. inline TRect
  1175. TRect::InflatedBy(int dx, int dy) const
  1176. {
  1177.   return TRect(left-dx, top-dy, right+dx, bottom+dy);
  1178. }
  1179.  
  1180. //
  1181. // Inflate the size of the rectangle.
  1182. //
  1183. inline TRect
  1184. TRect::InflatedBy(const TSize& size) const
  1185. {
  1186.   return InflatedBy(size.cx, size.cy);
  1187. }
  1188.  
  1189. //
  1190. // Normalize the rectangle, such that left is less than  right and
  1191. // top is less than bottom.
  1192. //
  1193. inline TRect
  1194. TRect::Normalized() const
  1195. {
  1196.   return TRect(Min(left, right), Min(top, bottom),
  1197.                Max(left, right), Max(top, bottom));
  1198. }
  1199.  
  1200. //
  1201. // Return the intersection of two rectangles.
  1202. //
  1203. inline TRect
  1204. TRect::operator &(const TRect& other) const
  1205. {
  1206.   if (Touches(other))
  1207.     return TRect(Max(left, other.left), Max(top, other.top),
  1208.                  Min(right, other.right), Min(bottom, other.bottom));
  1209.   return TRect(0, 0, 0, 0);
  1210. }
  1211.  
  1212. //
  1213. // Return the union of two rectangles.
  1214. //
  1215. inline TRect
  1216. TRect::operator |(const TRect& other) const
  1217. {
  1218.   return TRect(Min(left, other.left), Min(top, other.top),
  1219.                Max(right, other.right), Max(bottom, other.bottom));
  1220. }
  1221.  
  1222. //
  1223. // Move the rectangle by delta.
  1224. //
  1225. inline TRect&
  1226. TRect::operator +=(const TSize& delta)
  1227. {
  1228.   Offset(delta.cx, delta.cy);
  1229.   return *this;
  1230. }
  1231.  
  1232. //
  1233. // Move the rectangle by negative delta.
  1234. //
  1235. inline TRect&
  1236. TRect::operator -=(const TSize& delta)
  1237. {
  1238.   return *this += -delta;
  1239. }
  1240.  
  1241. //
  1242. // Move the upper left corner of the rectangle to a new location and
  1243. // maintain the current dimension.
  1244. //
  1245. inline TRect&
  1246. TRect::MoveTo(int x, int y)
  1247. {
  1248.   right = x + Width();
  1249.   bottom = y + Height();
  1250.   left = x;
  1251.   top = y;
  1252.   return *this;
  1253. }
  1254.  
  1255. //
  1256. // Inflate the size of the rectangle by delta.
  1257. //
  1258. inline TRect&
  1259. TRect::Inflate(const TSize& delta)
  1260. {
  1261.   return Inflate(delta.cx, delta.cy);
  1262. }
  1263.  
  1264. //----------------------------------------------------------------------------
  1265. // TSize
  1266. //
  1267.  
  1268. //
  1269. // Default constructor that does nothing.
  1270. //
  1271. inline
  1272. TSize::TSize()
  1273. {
  1274.  
  1275. }
  1276.  
  1277. //
  1278. // Constructor to initialize the size.
  1279. //
  1280. inline
  1281. TSize::TSize(int dx, int dy)
  1282. {
  1283.   cx = dx;
  1284.   cy = dy;
  1285. }
  1286.  
  1287. //
  1288. // Constructor to initialize from an existing point.
  1289. //
  1290. inline
  1291. TSize::TSize(const tagPOINT far& point)
  1292. {
  1293.   cx = point.x;
  1294.   cy = point.y;
  1295. }
  1296.  
  1297. //
  1298. // Constructor to initialize from an existing size.
  1299. //
  1300. inline
  1301. TSize::TSize(const tagSIZE far& size)
  1302. {
  1303.   cx = size.cx;
  1304.   cy = size.cy;
  1305. }
  1306.  
  1307. //
  1308. // Constructor to initialize from an existing size.
  1309. //
  1310. inline
  1311. TSize::TSize(const TSize far& size)
  1312. {
  1313.   cx = size.cx;
  1314.   cy = size.cy;
  1315. }
  1316.  
  1317. //
  1318. // Constructor that assumes the high-word of a long is the width
  1319. // and the low-word of a long is the height.
  1320. //
  1321. inline
  1322. TSize::TSize(uint32 dw)
  1323. {
  1324.   cx = LoUint16(dw);
  1325.   cy = HiUint16(dw);
  1326. }
  1327.  
  1328. //
  1329. // Return the width.
  1330. //
  1331. inline int
  1332. TSize::X() const
  1333. {
  1334.   return cx;
  1335. }
  1336.  
  1337. //
  1338. // Return the height.
  1339. //
  1340. inline int
  1341. TSize::Y() const
  1342. {
  1343.   return cy;
  1344. }
  1345.  
  1346. //
  1347. //
  1348. //
  1349. inline TSize
  1350. TSize::operator -() const
  1351. {
  1352.   return TSize(-cx, -cy);
  1353. }
  1354.  
  1355. //
  1356. // Return true if the sizes are identical.
  1357. //
  1358. inline bool
  1359. TSize::operator ==(const TSize& other) const
  1360. {
  1361.   return ToBool(other.cx==cx && other.cy==cy);
  1362. }
  1363.  
  1364. //
  1365. // Return true if the sizes are not identical.
  1366. //
  1367. inline bool
  1368. TSize::operator !=(const TSize& other) const
  1369. {
  1370.   return ToBool(other.cx!=cx || other.cy!=cy);
  1371. }
  1372.  
  1373. //
  1374. // Increase the width and height by size.
  1375. //
  1376. inline TSize
  1377. TSize::operator +(const TSize& size) const
  1378. {
  1379.   return TSize(cx+size.cx, cy+size.cy);
  1380. }
  1381.  
  1382. //
  1383. // Decrease the width and height by size.
  1384. //
  1385. inline TSize
  1386. TSize::operator -(const TSize& size) const
  1387. {
  1388.   return TSize(cx-size.cx, cy-size.cy);
  1389. }
  1390.  
  1391. //
  1392. // Increase the width and height by size.
  1393. //
  1394. inline TSize&
  1395. TSize::operator +=(const TSize& size)
  1396. {
  1397.   cx += size.cx;
  1398.   cy += size.cy;
  1399.   return *this;
  1400. }
  1401.  
  1402. //
  1403. // Decrease the width and height by size.
  1404. //
  1405. inline TSize&
  1406. TSize::operator -=(const TSize& size)
  1407. {
  1408.   cx -= size.cx;
  1409.   cy -= size.cy;
  1410.   return *this;
  1411. }
  1412.  
  1413. #endif  // WINSYS_GEOMETRY_H
  1414.